﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Linq;
namespace WPFExcelReport
{
    public partial class TABWB14
    {
        /// <summary>
        /// 1.实现根据采购单号，查询出面料的信息，然后产生入库记录
        /// 2.功能和流程：输入采购单号，查询品种色号和数量，
        /// 3.输入布卷数和布卷长度
        /// 4.产生布卷清单，入库的仓库
        /// 5.确认后，执行WBBST等一系列的工作
        /// 6.检查预留情况，根据预留，将布卷完成到目的地的留位
        /// </summary>
        /// 


        #region ==dwEKP===========
        public static readonly DependencyProperty dwEKPProperty = DependencyProperty.Register("dwEKP",
            typeof(DataRow), typeof(TABWB14),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwEKPChanged));
        public DataRow dwEKP
        {
            get
            {
                return (DataRow)GetValue(dwEKPProperty);
            }
            set
            {
                SetValue(dwEKPProperty, value);
            }
        }
        private static void OndwEKPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TABWB14;
            if (src != null)
            {
                src.dwEKP = (DataRow)e.NewValue;
            }
        }
        #endregion 

        #region ==TLG===========
        public static readonly DependencyProperty TLGProperty = DependencyProperty.Register("TLG",
            typeof(double), typeof(TABWB14),
            new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTLGChanged));
        public double TLG
        {
            get
            {
                return (double)GetValue(TLGProperty);
            }
            set
            {
                SetValue(TLGProperty, value);
            }
        }
        private static void OnTLGChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TABWB14;
            if (src != null)
            {
                src.TLG = (double)e.NewValue;
            }
        }
        #endregion

        #region ==IsChgWB===========
        public static readonly DependencyProperty IsChgWBProperty = DependencyProperty.Register("IsChgWB",
            typeof(bool), typeof(TABWB14),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsChgWBChanged));
        public bool IsChgWB
        {
            get
            {
                return (bool)GetValue(IsChgWBProperty);
            }
            set
            {
                SetValue(IsChgWBProperty, value);
            }
        }
        private static void OnIsChgWBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TABWB14;
            if (src != null)
            {
                src.IsChgWB = (bool)e.NewValue;
            }
        }
        #endregion

        public TABWB14()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_WBBEW();
            this.DataContext = MV;
            InitializeComponent();
        }

        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            EXAUF = "";
            EXAUF = "V" + KRS + LFD + POS;
        }

        //根据是否该单已经有入库
        bool IsNewEkIn = false;

        public void GETSEQ()
        {
            var qMU = from MUrow in TBMain.AsEnumerable()
                      group MUrow by new
                      {
                          KRS = MUrow["WBEWRWKRS"].ToString(),
                          LFD = MUrow["WBEWRWLFD"].ToString(),
                      }
                          into g
                          select new
                          {
                              KEY = g.Key,
                              WBEWLFDNR = g.Max(XD => Convert.ToInt16(XD["WBEWPOS"].ToString())),
                          };
            foreach (var mu in qMU)
            {

                WPOS = mu.WBEWLFDNR;
            }

        }

        public override void NewCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                if (dwEKP == null)
                {
                    MessageBox.Show("必须根据采购单才能入库");
                    EXAUF = "B" + KRS + LFD + POS;
                    return;
                }
                if (!IsNew)
                {
                    IsNew = true;
                    TLG = 0.01 * Convert.ToDouble(dwEKP["ABPSOLLMENGE1"].ToString());
                    return;
                }
                IsNewEkIn = false;
                if (TBMain.Rows.Count == 0)
                    IsNewEkIn = true;
                //if (IsNewEkIn)
                {
                    TMV.NEWABWB(dwEKP, LO, LB, LP, RLG, ROLLS,0.0);
                }
                IsNew = false;
                base.NewCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void NewWBCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwEKP == null)
                {
                    MessageBox.Show("必须根据采购单才能入库");
                    EXAUF = "V" + KRS + LFD + POS;
                    return;
                }
                if (!IsNew)
                {
                    IsNew = true;
                    RLG = 0;
                    return;
                }
                if (RLG == 0)
                {
                    MessageBox.Show("请输入布卷长度");
                    return;
                }
                TMV.ADDEKWB(RLG);
                IsNew = false;
                RLG = 0;
                base.NewCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void EditCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (cWB.TBMain.Rows.Count > 0)
            {
               TMV.TBWB = cWB.TBMain.Copy();
            }

            base.EditCommand(sender, e);
        }

        public override void DeleteCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            string STKNR = dwMaster["WBEWSTKNR"].ToString();
            string rwauf = dwMaster["WBEWRWKRS"].ToString() + dwMaster["WBEWRWLFD"].ToString();
            vwMaster.View.MoveCurrentTo(-1);
            filter = "WBEWSTKNR='" + STKNR + "'";
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                TMV.DeleteICC_WBBEW(row,1);
                TMV.VWBBEW.DeleteWBBEW(row, 1);
                row.Delete();
            }
            TBMain.AcceptChanges();
            if (TMV.TBWB == null)
            {
                return;
            }
            filter = "WBBSTKNR='" + STKNR + "'";

            rows = TMV.TBWB.Select(filter);
            foreach (DataRow row in rows)
            {
                TMV.VWB.DeleteWBBST(row, 1);
                row.Delete();
            }
            ((VICC_WBBEW)MV).TBWB.AcceptChanges();
            INFO= "注意，需要执行一下保存，以便刷新数据若是布卷 ";
            INFO +=Environment.NewLine+ "注意， 已经留位，则需要手工更改需求数量。";
        }

        public override void LOADBYAUF()
        {
            TBMain.Clear();
            if (ORDERNUM.Length < 11)
                return;
            KRS = ORDERNUM.Substring(0, 2);
            LFD = ORDERNUM.Substring(2, 6);
            POS = ORDERNUM.Substring(8, 3);
            base.LOADBYAUF();
        }

        public override void SaveCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                PM["ORDERNUM"] = EXAUF;
                PM["AUFTYP"] = "B";
                PM["RWAUF"] = TMV.RWAUF;
                PM["KRS"] =KRS;
                PM["LFD"] = LFD;
                PM["POS"] = POS;
                PM["TBWB14"] = TBMain;
                TMV.POSTBATCH(PM);
                //TMV.VEKP.UpdateEKPOS(dwEKP, 15);
                TMV.VEKP.UpdateEKPOS(dwEKP, 12);
              
                string key = EXAUF;
                EXAUF = "";
                EXAUF = "V"+KRS+LFD+POS;
                /*  key = RWAUF;
                RWAUF = "";
                RWAUF = key;*/
                base.SaveCommand(sender, e);
                INFO = "已保存";
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public void ChgLGCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                MessageBox.Show("请先选择布卷");
                return;
            }
            if (!IsChgWB)
            {
                IsChgWB = true;
                RLG = 0;
                return;
            }
            string stknr = dwMaster["WBEWSTKNR"].ToString();
            ((VICC_WBBEW)MV).CHGEKWB(stknr, RLG);
            RLG = 0;
            IsChgWB = false;
        }

        public override void OnRWAUFChg(string value)
        {
            TBMain.Clear();
            if (value.Length < 8)
                return;
            TMV.RWAUF = value;
            dwData = TBMain.Clone().NewRow();
            dwData["WBEWMANDANT"] = app.Mandant;
            dwData["WBEWRWKRS"] = value.Substring(0, 2);
            dwData["WBEWRWLFD"] = value.Substring(2, 6);

            SResult = TMV.QueryICC_WBBEW(dwData, 4);
            MergeData(SResult, true, true);
            if (TBMain.Rows.Count > 0)
            {
                return;
            }
            SResult = TMV.VWBBEW.QueryWBBEW(dwData, 12);
            SResult.Tables[0].TableName = MV.TableName;
            MergeData(SResult, true, true);
            base.OnRWAUFChg(value);
        }

    }
}
